package com.pctx.bibacrmrbac.service.impl;

import com.pctx.bibacrmrbac.dao.RolePermissionMapper;
import com.pctx.bibacrmrbac.pojo.RolePermission;
import com.pctx.bibacrmrbac.pojo.RolePermissionExample;
import com.pctx.bibacrmrbac.service.RolePermissionService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chason on 2018/2/26.10:55
 */
@Service("rolePermissionService")
public class RolePermissionServiceImpl implements RolePermissionService {
    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public boolean addRolePermissions(String roleUuid, String permissionUuids) throws Exception {
        if (StringUtils.isEmpty(roleUuid) || StringUtils.isEmpty(permissionUuids))
            return false;

        String[] permissions = permissionUuids.split(",");
        List<RolePermission> list = new ArrayList<RolePermission>();
        for (String permission : permissions) {
            RolePermission rp = new RolePermission();
            rp.setRoleUuid(roleUuid);
            rp.setPermissionUuid(permission);
            list.add(rp);
        }

        return rolePermissionMapper.insertBatch(list) > 0;
    }

    @Override
    public boolean delByRoleUuid(String roleUuid) throws Exception {
        if (StringUtils.isEmpty(roleUuid))
            return false;

        RolePermissionExample example = new RolePermissionExample();
        RolePermissionExample.Criteria criteria = example.createCriteria();
        criteria.andRoleUuidEqualTo(roleUuid);

        return rolePermissionMapper.deleteByExample(example) > 0;
    }
}
